<?php

/**
 * Created by PhpStorm.
 * User: hda
 * Date: 2017/5/20
 * Time: 上午11:43
 */

namespace app\user\controller;

use icontroller\wechat\AController;
use logic\wechat\index\LUserAddress;
use think\Request;

class Address extends AController
{
    private $logic;

    function __construct()
    {
        parent::__construct();
        $this->logic = new LUserAddress();
    }

    public function index()
    {
        $list = $this->logic->select($this->account_id, 'id,name,mobile,province,city,district,address,flag_default');
        if ($list) {
            $this->assign('list', json_encode($list));
        } else {
            $this->assign('list');
        }
        return $this->fetch();
    }

    public function select()
    {
        $list = $this->logic->select($this->account_id, 'id,name,mobile,province,city,district,address,flag_default');
        if ($list) {
            $this->assign('list', json_encode($list));
        } else {
            $this->assign('list');
        }
        return $this->fetch();
    }

    public function select_data()
    {
        $list = $this->logic->select($this->account_id);
        return $list;
    }

    public function post()
    {
        $addres_id = input('id');
        $post_data = [
            'account_id' => $this->account_id,
            'name' => input('name'),
            'mobile' => input('mobile'),
            'province' => input('province',""),
            'city' => input('city',""),
            'district' => input('district',""),
            'address' => input('address'),
            'zip_code' => input('zip_code'),
            'lng' => input('lng'),
            'lat' => input('lat'),
            'zoom' => input('zoom')
        ];
        return $this->logic->post($addres_id, $post_data, $this->account_id);
    }

    public function edit()
    {
        $address_id = input('id');
        $m = null;
        if ($address_id) {
            $m = $this->logic->find($address_id, $this->account_id);
        }
        $this->assign('m', $m);
        return $this->fetch();
    }

    public function location()
    {
        return $this->fetch();
    }

    public function map()
    {
        return $this->fetch();
    }

    //设置默认收货地址
    public function set_default()
    {
        $address_id = input('id');
        return $this->logic->set_default($address_id, $this->account_id);
    }

    public function delete()
    {
        $address_id = input('post.id');
        return $this->logic->delete($address_id, $this->account_id);
    }
}